﻿using System;
using System.Text.RegularExpressions;

namespace Common.Tools
{
    public class RegexLib
    {
        #region 数值验证
        /// <summary>
        /// 验证是否为小数
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>true:是；fasel:否</returns>
        public static bool IsValidDecimal(string str)
        {
            return str != null && Regex.IsMatch(str, "^[\\+|\\-]?\\d+\\.\\d+$");
        }
        /// <summary>
        /// 验证是否为小数
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>true:是；fasel:否</returns>
        public static bool IsValidDecimalOrNum(string str)
        {
            return str != null && Regex.IsMatch(str, "^[\\+|\\-]?\\d+(\\.\\d+)?$");
        }
        /// <summary>
        /// 判断是否为数值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumber(string str)
        {
            return str != null && Regex.IsMatch(str, "^[\\+|\\-]?\\d+$");
        }
        /// <summary>
        /// 判断是否为整数int
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInt(string str)
        {
            if (IsNumber(str))
            {
                bool bol = str.TrimStart('+', '-').TrimStart('0').Length < int.MaxValue.ToString().Length;
                if (bol)
                {
                    str = str.TrimStart('+', '-');
                    return Convert.ToInt64(str) < int.MaxValue;
                }
            }
            return false;
        }
        /// <summary>
        /// 判断是否为正整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsPositive(string str)
        {
            return str != null && Regex.IsMatch(str, "^(\\+)?\\d+$");
        }
        /// <summary>
        /// 判断是否为正整数,但不包括0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsBarringZeroPositive(string str)
        {
            return str != null && Regex.IsMatch(str, "^(\\+)?[1-9]\\d*$");
        }
        #endregion


        #region 格式验证
        /// <summary>
        /// 是否中文(不带标点符号,特殊符号,可以是偏旁部首)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsChinese(string str)
        {
            return Regex.IsMatch(str, IsChineseReg);
        }
        public static bool IsDoubleByte(string str)
        {
            return Regex.IsMatch(str, IsDoubleByteReg);
        }
        /// <summary>
        /// 验证是否为手机号码格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMobileNO(string str)
        {
            return Regex.IsMatch(str, "^1[3-8]\\d{9}$");
        }
        /// <summary>
        /// 验证是否Email地址
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(string str)
        {
            return Regex.IsMatch(str, "^\\w+([-+.]\\w+)*@\\w+(-\\w+)*((\\.)(\\w+(-\\w+)*))+$");
        }
        /// <summary>
        /// 验证是否Url地址
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUrl(string str)
        {
            return Regex.IsMatch(str, "^((http://)|(https://))?(\\w+(-\\w+)*)((\\.|\\:)(\\w+(-\\w+)*))*(/?\\S*)?$");
        }
        /// <summary>
        /// 验证身份证号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIDCard(string str)
        {
            return Regex.IsMatch(str, "^\\d{6}((((1\\d{3})|(20\\d{2}))(((01)|(03)|(05)|(07)|(08)|(10)|(12))(([012]\\d)|3[0-1])|((04)|(06)|(09)|11)(([012]\\d)|30)|(02)([012]\\d))\\d{3}(\\d|x))|((\\d{2})(((01)|(03)|(05)|(07)|(08)|(10)|(12))(([012]\\d)|3[0-1])|((04)|(06)|(09)|11)(([012]\\d)|30)|(02)([012]\\d))\\d{3}))$");
        }
        #endregion

        #region 格式验证正则表达式
        private const string IsChineseReg = "([\u4e00-\u9fa5])+";
        private const string IsDoubleByteReg = "[^\\x00-\\xff]+";
        #endregion
    }
}
